
//
//  DiscoverMCircleDetailViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/14.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "DiscoverHouseholdCommentsViewController.h"
#import "RDVTabBarController.h"
#import "UIImageView+WebCache.h"
#import "HttpRequest.h"
#import "LoginViewController.h"
#import "UIButton+WebCache.h"
#import "MCircleCell.h"
#import "KeyboardModel.h"
@interface DiscoverHouseholdCommentsViewController ()<UIScrollViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITableViewDataSource,UITableViewDelegate,UITextViewDelegate,KeyboardModelDelegate>

@property (nonatomic, strong) UIImageView * allImageBtn;
@property(nonatomic,strong)NSString*sta;
@end

@implementation DiscoverHouseholdCommentsViewController{
    int index;
    int tapImageCount;
    UIScrollView*MYmainScroll;
    NSMutableArray*arr;
    UIView*view;
    NSMutableArray *cDate;
    NSMutableArray *content;
    NSMutableArray *headImage;
    NSMutableArray *userName;
    UITextView*tv;
    UITableView *table;
    NSString*Education;
    NSDictionary*dic;
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
//    self.navigationController.delegate = nil;
    
}
-(void)BackAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.title = @"资讯详情";
    self.title = @"详情";
    self.navigationController.delegate = self;
    
//    self.navigationController.interactivePopGestureRecognizer.delegate = self;
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
//    [[self rdv_tabBarController] setTabBarHidden:YES];
//    self.navigationController.navigationBarHidden = NO;
//    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
//    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
//    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
//    [self.navigationController.navigationBar addSubview:lineLabel];
    
//    UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 0,ScreenWidth, 43)];
//    [lineLabel1 setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
//    lineLabel1.textAlignment = NSTextAlignmentCenter;
//    lineLabel1.text = @"123";
//    [self.navigationController.navigationBar addSubview:lineLabel1];
    
//    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
//    [[UINavigationBar appearance] setBarTintColor:Color];
//
//    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
//    CGRect backframe = CGRectMake(0,0,30,30);
//    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
//    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
//    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
//    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
//    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
//    self.view.backgroundColor=[UIColor whiteColor];
    [self setup];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:table];
    UIView*tableFooter1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    tableFooter1.backgroundColor=[UIColor whiteColor];
    table.tableFooterView=tableFooter1;
}

- (void)setup{
    
    content=[[NSMutableArray alloc]init];
    userName=[[NSMutableArray alloc]init];
    headImage=[[NSMutableArray alloc]init];
    cDate=[[NSMutableArray alloc]init];
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:self.fid forKey:@"infoId"];
    
    NSLog(@"-----%@",params);
    [MBProgressHUD showIndeterminateHudSub:@"" delay:20 view:self.view];
    [HttpRequest requestData:@"media/Household/Comments" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"-----%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                
                
                 [[KeyboardModelboard customKeyboard]textViewShowView:self String1:self.isCollect pinglunshuString1 :[severDataDict.dataDict objectForKey:@"TotalCount"]   customKeyboardDelegate:self];
                
                dic=severDataDict.dataDict;
                arr=[[severDataDict.dataDict objectForKey:@"mcircle"]objectForKey:@"images"];
                NSMutableArray* arrr=[[NSMutableArray alloc]init];
                
                arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                UIButton*sxBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  180, 60/2)];
                [sxBtn setTitle:[NSString stringWithFormat:@"全部评论 %ld 条",  arrr.count] forState:UIControlStateNormal];
                [sxBtn setTitleColor:zhonghuiColor forState:UIControlStateNormal];
                sxBtn .titleLabel.font=[UIFont systemFontOfSize:15];
                sxBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
                UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:sxBtn];
                self.navigationItem.rightBarButtonItem = myBtnBtn;
                for (NSDictionary* dicc in arrr ) {
                    
                    [userName addObject:[dicc valueForKey:@"creatorName"]];
                    [content addObject:[dicc valueForKey:@"content"]];
                    [headImage addObject:[dicc valueForKey:@"headImage"]];
                    
                    NSString* string =  [dicc valueForKey:@"createTime"];
                    NSLog(@"-------------%@",string);
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
                    NSDate *destDate= [dateFormatter dateFromString:string];
                    NSTimeInterval  timeInterval = [destDate timeIntervalSinceNow];
                    timeInterval = -timeInterval;
                    NSString *result;
                    long temp = 0;
                    if (timeInterval < 60) {
                        result = [NSString stringWithFormat:@"刚刚"];
                    }else if((temp = timeInterval/60) <60){
                        result = [NSString stringWithFormat:@"%ld分前",temp];
                    }else if((temp = temp/60) <24){
                        result = [NSString stringWithFormat:@"%ld小时前",temp];
                    }else if((temp = temp/24) <30){
                        result = [NSString stringWithFormat:@"%ld天前",temp];
                    }else if((temp = temp/30) <12){
                        result = [NSString stringWithFormat:@"%ld月前",temp];
                    }else{
                        temp = temp/12;
                        result = [NSString stringWithFormat:@"%ld年前",temp];
                    }
                    [cDate addObject:result];
                }
                [table reloadData];
                
            }else{
                
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    
                    LoginViewController*detailViewController = [[LoginViewController alloc] init];
                    
                    [self.navigationController pushViewController:detailViewController animated:YES];
                    
                }
            }
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];

}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [userName count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    MCircleCell* cell = (MCircleCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"MCircleCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.image.layer.masksToBounds = YES;
    cell.image.layer.cornerRadius = 40/2;
    [cell.image setImageWithURL:[NSURL URLWithString:[headImage objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"damorentouxiang"]];
    cell.name.text =[NSString stringWithFormat:@"%@",  [userName objectAtIndex:indexPath.row]];
    NSString *s2 =[content objectAtIndex:indexPath.row];
    UIFont *font2 = [UIFont systemFontOfSize:14];
    CGSize size2 = CGSizeMake(ScreenWidth-40-40, 2000);
    CGSize labelsize2 = [s2 sizeWithFont:font2 constrainedToSize:size2 lineBreakMode:NSLineBreakByCharWrapping];
    
    Education=[NSString stringWithFormat:@"%f", labelsize2.height+20];
    cell.content.frame=CGRectMake(60, 50, ScreenWidth-40-40,labelsize2.height+5);
    cell.content.text =[NSString stringWithFormat:@"%@",  [content objectAtIndex:indexPath.row]];
    cell.content.textColor=qianheiColor;
    
    cell.time.text =[NSString stringWithFormat:@"%@",[cDate objectAtIndex:indexPath.row]];
    
   
       
        UIImageView *_hengxian = [[UIImageView alloc]initWithFrame:CGRectMake(10,Education.intValue+150/3-0.45, ScreenWidth-20, 0.45)];
        _hengxian.image=[UIImage imageNamed:@"hengxian"];
        [cell.contentView addSubview:_hengxian];

    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return Education.intValue+150/3;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

-(void)pinglunBtn:(UITextField *)textViewGet
{
    NSLog(@"%@",textViewGet.text);
    if (textViewGet.text.length==0)
    {
        
        [TSAlertTool showAlertMessage:@"内容为空"];
    }else{
        if ([textViewGet.text isEqualToString:@"我来说两句..."])
        {
            [TSAlertTool showAlertMessage:@"内容为空"];
        }else{
            NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
            NSString* tokenId = [user1 objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:tokenId forKey:@"tokenId"];
            [params setValue:self.fid forKey:@"infoId"];
            [params setValue:textViewGet.text forKey:@"content"];
            NSLog(@"-----%@",params);
            [MBProgressHUD showIndeterminateHudSub:@"" delay:10 view:self.view];
            [HttpRequest requestData:@"media/Household/AddComment" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    [MBProgressHUD hideProgressHUDInternal:self.view];
                    NSLog(@"-----%@",severDataDict.dataDict);
                    textViewGet.text=@"";
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        
                            [self setup];

                    }else{
                        [MBProgressHUD hideProgressHUDInternal:self.view];
                       
                    }
                }
                else
                {
                    [MBProgressHUD hideProgressHUDInternal:self.view];
                    [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
                }
                
            }];}
        
    }
}
-(void)pinglunBtnjiemian:(NSString *)pinglunjiemian{
    
//    DiscoverHouseholdCommentsViewController*detailViewController = [[DiscoverHouseholdCommentsViewController alloc] init];
//    detailViewController.fid=self.fid;
//    [self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)guangzhuBtn:(UIButton *)guanzhu
{
    NSLog(@"%@",guanzhu.titleLabel.text);
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString* tokenId = [user1 objectForKey:@"tokenId"];
    NSMutableDictionary *params = [NSMutableDictionary new];
    [params setValue:tokenId forKey:@"tokenId"];
    [params setValue:self.fid forKey:@"infoId"];
    
    NSLog(@"-----%@",params);
    [MBProgressHUD showIndeterminateHudSub:@"" delay:10 view:self.view];
    [HttpRequest requestData:@"media/Household/Collect" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            NSLog(@"-----%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                
                [[KeyboardModelboard customKeyboard]textViewShowView:self String1:[severDataDict.dataDict objectForKey:@"isCollect"] pinglunshuString1 :[dic  objectForKey:@"TotalCount"]    customKeyboardDelegate:self];
                
            }else{
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];}}
        else
        {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            
            [MBProgressHUD showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}

@end
